package com.lyj.controller;

import com.lyj.pojo.Result;
import com.lyj.pojo.Room;
import com.lyj.pojo.dto.RoomEditDTO;
import com.lyj.pojo.dto.RoomAddDTO;
import com.lyj.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    /**
     * 获取房间列表
     */
    @GetMapping("/list")
    public Result getRoomList(@RequestParam Integer familyId) {
        List<Room> roomList = roomService.getRoomList(familyId);
        return Result.success(roomList);
    }

    /**
     * 添加房间
     */
    @PostMapping("/add")
    public Result addRoom(@Valid @RequestBody RoomAddDTO roomAddDTO) {
        Integer roomId = roomService.addRoom(roomAddDTO);
        Map<String, Integer> result = new HashMap<>();
        result.put("roomId", roomId);
        return Result.success(result);
    }

    /**
     * 编辑房间
     */
    @PutMapping("/edit")
    public Result editRoom(
            @RequestParam Integer familyId,
            @Valid @RequestBody RoomEditDTO roomEditDTO) {
        roomService.editRoom(roomEditDTO, familyId);
        return Result.success("房间修改成功");
    }

    /**
     * 删除房间
     */
    @DeleteMapping("/{roomId}")
    public Result deleteRoom(
            @PathVariable Integer roomId,
            @RequestParam Integer familyId) {
        roomService.deleteRoom(roomId, familyId);
        return Result.success("房间删除成功");
    }
}
